
package org.itboys.mobile.service.mongo.web;

import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.mobile.entity.mongo.SundayPermission;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_权限_service
 */
@Service
public class SundayPermissionService extends BaseMongoService<SundayPermission> {


	@Autowired
	private ResourceHolder resourceHolder;
	
	private static final long serialVersionUID = -5806434055381127192L;
	@Resource(name="mobileDs")
	private MongoDataSource sundayDataSource;
	@Override
	protected MongoDataSource getMongoDataSource() {
		return sundayDataSource;
	}
	@Override
	protected Class<SundayPermission> getEntityClass() {
		return SundayPermission.class;
	}


	/**
	 * 保存权限
	 * @param memberId
	 * @param otherIds
	 * @param otherId
	 * @param type 1,商品，2分类，3其他
	 * @param isOverwrite
	 */
	public void savePermission(Long memberId,List<Long> otherIds,Long otherId,Integer type,boolean isOverwrite){
			if(ListUtil.isNull(otherIds))otherIds=new ArrayList<Long>();
			if(LongUtil.isNotZreo(otherId))otherIds.add(otherId);
			if(isOverwrite){
				Map<String,Object> param = new HashMap<String,Object>();
				param.put("isDeleted",0);
				param.put("type",type);
				param.put("memberId",memberId);
				super.delete(param);
			}
			if(ListUtil.isNotNull(otherIds)){
				List<SundayPermission> permissions = new ArrayList<SundayPermission>();
				for(Long o:otherIds){
					permissions.add(new SundayPermission(memberId,o,type));
				}

				if(ListUtil.isNotNull(permissions))super.batchSave(permissions);
			}
	}

	/**
	 *获取权限
	 * @param memberId
	 * @param type
	 * @return
	 */
	public List<Long> getPermission(Long memberId, Integer type){
			Map<String,Object> param = new HashMap<String,Object>();
			param.put("isDeleted",0);
			param.put("type",type);
			param.put("memberId",memberId);
			List<Long> otherIds=new ArrayList<Long>();

			for(SundayPermission permission:super.list(param)){
				otherIds.add(permission.getOtherId());
			}
			return otherIds;
	}
	
}

